package com.winderinfo.yikaotianxia.ui.order;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;

import java.util.List;


public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final String WEIZHIFU = "0";
    private final String DAIFAHUO = "1";
    private final String DAISHOUHUO = "2";

    private Context context;
    private List<OrderBean.YkOrderListBean> list;

    public OrderAdapter(Context context, List<OrderBean.YkOrderListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getOrderStatus().equals(WEIZHIFU)) {
            return 0;//未支付
        } else if (list.get(position).getOrderStatus().equals(DAIFAHUO)) {
            return 1;//代发货
        } else if (list.get(position).getOrderStatus().equals(DAISHOUHUO)) {
            return 2;//待收货
        } else {
            return 3;//已完成
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == 0) {//未支付
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_tobepaid, viewGroup, false);
            SelectedViewHolder selectedViewHolder = new SelectedViewHolder(view);
            return selectedViewHolder;
        } else if (i == 1) {//代发货
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_delivery, viewGroup, false);
            DaiFaHuoHolder daiFaHuoHolder = new DaiFaHuoHolder(view);
            return daiFaHuoHolder;
        } else if (i == 2) {//待收货
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_received, viewGroup, false);
            DaiShouHuoHolder daiShouHuoHolder = new DaiShouHuoHolder(view);
            return daiShouHuoHolder;
        } else if (i == 3) {//已完成
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_completed, viewGroup, false);
            YiWanChengHolder wanChengHolder = new YiWanChengHolder(view);
            return wanChengHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (list.get(i).getOrderStatus().equals("0")) {//未支付
            if (viewHolder instanceof SelectedViewHolder) {
                SelectedViewHolder selectedViewHolder = (SelectedViewHolder) viewHolder;
                selectedViewHolder.to_carde.setText("订单号:  " + list.get(i).getOrderNo());
                selectedViewHolder.to_price.setText("支付金额:  " + list.get(i).getPayMoney());
                if (list.get(i).getPayType().equals("0")) {
                    selectedViewHolder.to_fangshi.setText("支付方式:  微信");
                }
                if (list.get(i).getPayType().equals("1")) {
                    selectedViewHolder.to_fangshi.setText("支付方式:  支付宝");
                }
                selectedViewHolder.to_time.setText("下单时间:  " + list.get(i).getCreateTime());
                //查看订单详情
                selectedViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onDetails != null) {
                            //订单Id
                            int orderid = list.get(i).getId();
                            onDetails.getOrederId(orderid);
                        }
                    }
                });
            }
        } else if (list.get(i).getOrderStatus().equals("1")) {//代发货
            if (viewHolder instanceof DaiFaHuoHolder) {
                DaiFaHuoHolder daiFaHuoHolder = (DaiFaHuoHolder) viewHolder;
                daiFaHuoHolder.de_corde.setText("订单号:  " + list.get(i).getOrderNo());
                daiFaHuoHolder.de_price.setText("支付金额:  " + list.get(i).getPayMoney());
                if (list.get(i).getPayType().equals("0")) {
                    daiFaHuoHolder.de_fangshi.setText("支付方式:  微信");
                }
                if (list.get(i).getPayType().equals("1")) {
                    daiFaHuoHolder.de_fangshi.setText("支付方式:  支付宝");
                }
                daiFaHuoHolder.de_time.setText("下单时间:  " + list.get(i).getCreateTime());
                //提醒发货
                daiFaHuoHolder.de_xiangqin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtil.showSuccess(context, "提醒发货成功");
                    }
                });
                //查看订单详情
                daiFaHuoHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onDetails != null) {
                            //订单Id
                            int orderid = list.get(i).getId();
                            onDetails.getOrederId(orderid);
                        }
                    }
                });
            }
        } else if (list.get(i).getOrderStatus().equals("2")) {//待收货
            if (viewHolder instanceof DaiShouHuoHolder) {
                DaiShouHuoHolder daiShouHuoHolder = (DaiShouHuoHolder) viewHolder;
                daiShouHuoHolder.rec_corde.setText("订单号:  " + list.get(i).getOrderNo());
                daiShouHuoHolder.rec_price.setText("支付金额:  " + list.get(i).getPayMoney());
                if (list.get(i).getPayType().equals("0")) {
                    daiShouHuoHolder.rec_fangshi.setText("支付方式:  微信");
                }
                if (list.get(i).getPayType().equals("1")) {
                    daiShouHuoHolder.rec_fangshi.setText("支付方式:  支付宝");
                }
                daiShouHuoHolder.rec_time.setText("下单时间:  " + list.get(i).getCreateTime());
                //确认收货
                daiShouHuoHolder.rec_xiangqin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onShouHuo != null) {
                            //订单Id
                            int orderid = list.get(i).getId();
                            onShouHuo.getId(orderid);
                        }
                    }
                });
                //查看订单详情
                daiShouHuoHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onDetails != null) {
                            //订单Id
                            int orderid = list.get(i).getId();
                            onDetails.getOrederId(orderid);
                        }
                    }
                });
            }
        } else if (list.get(i).getOrderStatus().equals("3")) {//已完成
            if (viewHolder instanceof YiWanChengHolder) {
                YiWanChengHolder wanChengHolder = (YiWanChengHolder) viewHolder;
                wanChengHolder.com_corde.setText("订单号:  " + list.get(i).getOrderNo());
                wanChengHolder.com_price.setText("支付金额:  " + list.get(i).getPayMoney());
                if (list.get(i).getPayType().equals("0")) {
                    wanChengHolder.com_fangshi.setText("支付方式:  微信");
                }
                if (list.get(i).getPayType().equals("1")) {
                    wanChengHolder.com_fangshi.setText("支付方式:  支付宝");
                }
                wanChengHolder.com_time.setText("下单时间:  " + list.get(i).getCreateTime());
                //查看订单
                wanChengHolder.com_xiangqin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onDetails != null) {
                            //订单Id
                            int orderid = list.get(i).getId();
                            onDetails.getOrederId(orderid);
                        }
                    }
                });
                wanChengHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onDetails != null) {
                            //订单Id
                            int orderid = list.get(i).getId();
                            onDetails.getOrederId(orderid);
                        }
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //w未支付
    public class SelectedViewHolder extends RecyclerView.ViewHolder {

        private final TextView to_carde, to_price, to_fangshi, to_time, to_quxiao, to_zhifu;

        public SelectedViewHolder(@NonNull View itemView) {
            super(itemView);
            to_carde = itemView.findViewById(R.id.to_carde);
            to_price = itemView.findViewById(R.id.to_price);
            to_fangshi = itemView.findViewById(R.id.to_fangshi);
            to_time = itemView.findViewById(R.id.to_time);
            to_quxiao = itemView.findViewById(R.id.to_quxiao);
            to_zhifu = itemView.findViewById(R.id.to_zhifu);
        }
    }

    //代发货
    public class DaiFaHuoHolder extends RecyclerView.ViewHolder {

        private final TextView de_corde, de_price, de_fangshi, de_time, de_xiangqin;

        public DaiFaHuoHolder(@NonNull View itemView) {
            super(itemView);
            de_corde = itemView.findViewById(R.id.de_corde);
            de_price = itemView.findViewById(R.id.de_price);
            de_fangshi = itemView.findViewById(R.id.de_fangshi);
            de_time = itemView.findViewById(R.id.de_time);
            de_xiangqin = itemView.findViewById(R.id.de_xiangqin);
        }
    }

    //待收货
    public class DaiShouHuoHolder extends RecyclerView.ViewHolder {

        private final TextView rec_corde, rec_price, rec_fangshi, rec_time, rec_xiangqin;

        public DaiShouHuoHolder(@NonNull View itemView) {
            super(itemView);
            rec_corde = itemView.findViewById(R.id.rec_corde);
            rec_price = itemView.findViewById(R.id.rec_price);
            rec_fangshi = itemView.findViewById(R.id.rec_fangshi);
            rec_time = itemView.findViewById(R.id.rec_time);
            rec_xiangqin = itemView.findViewById(R.id.rec_xiangqin);
        }
    }

    //已完成
    public class YiWanChengHolder extends RecyclerView.ViewHolder {

        private final TextView com_corde, com_price, com_fangshi, com_time, com_xiangqin;

        public YiWanChengHolder(@NonNull View itemView) {
            super(itemView);
            com_corde = itemView.findViewById(R.id.com_corde);
            com_price = itemView.findViewById(R.id.com_price);
            com_fangshi = itemView.findViewById(R.id.com_fangshi);
            com_time = itemView.findViewById(R.id.com_time);
            com_xiangqin = itemView.findViewById(R.id.com_xiangqin);
        }
    }

    //确认收货
    public interface OnShouHuo {
        void getId(int orderId);
    }

    public OnShouHuo onShouHuo;

    public void setOnShouHuo(OnShouHuo onShouHuo) {
        this.onShouHuo = onShouHuo;
    }

    //查看订单
    public interface OnDetails {
        void getOrederId(int orderID);
    }

    public OnDetails onDetails;

    public void setOnDetails(OnDetails onDetails) {
        this.onDetails = onDetails;
    }
}
